package com.asana.resteasy.api.client;

import it.betservice.commonlibxml.cards.lobby.Request;
import it.betservice.commonlibxml.cards.lobby.Response;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import com.asana.resteasy.api.bean.AsanaPost;
import com.asana.resteasy.api.bean.Project;
import com.asana.resteasy.api.bean.Story;
import com.asana.resteasy.api.bean.Tag;
import com.asana.resteasy.api.bean.Task;
import com.asana.resteasy.api.bean.User;
import com.asana.resteasy.api.bean.Workspace;
import com.asana.resteasy.api.exception.AsanaException;
import com.asana.resteasy.api.util.AsanaSearchBy;
import com.google.gson.Gson;

public class TestAsana {
	
	private static QName _request_QNAME = new QName("","request");
	private static QName _response_QNAME = new QName("","response");
	private static final String key = "n3DEJPR.zh0CBzXwu5cm4fLl7fcmkBOG";
	
	private static final String idProject = "7519011950092";
	private static final String idWorkspace = "1309739803352";
	private static final String idTag = "7999464354510";
	
	
	public static void main(String[] args) {
		//asanaAllTest();
		//deleteAll();
		//testTicketing();
		//List<Task> tasks = new Asana(key).getTasks("8000702700502", AsanaSearchBy.TAG);
		//testTicketing();
		testStory();
	}
	
	private static void testTicketing(){
		Asana asana = new Asana(key);
		
		AsanaPost post = new AsanaPost();
		post.getData().put("workspace", idWorkspace);
		post.getData().put("projects", idProject);
		post.getData().put("name", "TEST API " + new Date().getTime());
		post.getData().put("assignee", "1309739803347");
		post.getData().put("notes", "Questa è una prova!");
		
		Task task = asana.createTask(post);
	}
	
	private static void testStory(){
		Asana asana = new Asana(key);
		String idTask = "8008854283549";
		asana.updateTaskStatus(idTask, false);
		
		asana.addComment(idTask, "Prova commento! \r\n " + new Date().getTime() );
	}	
	
	
	private static void asanaAllTest(){
		Asana asana = new Asana(key);
		// Prende me...
		/*
		User user = asana.getMe();
		System.out.println(new Gson().toJson(user));
		// Prende i progetti...
		List<Project> projects = asana.getProjects();
		System.out.println(new Gson().toJson(projects));
		// Prende gli utenti...
		List<User> users = asana.getUsers();
		System.out.println(new Gson().toJson(users));
		// Prende un task...
		Task task = asana.getTask("7825284253219");
		System.out.println(new Gson().toJson(task));
		// Prende tutti i task di un progetto...
		List<Task> tasks = asana.getTasks(idProject, AsanaSearchBy.PROJECT);
		System.out.println(new Gson().toJson(tasks));
		// Prende tutti i task di un progetto...
		List<Tag> tags = asana.getTags();
		System.out.println(new Gson().toJson(tags));
		// Crea un TAG
		Tag tag = asana.createTag(idWorkspace, "Entita #0");
		System.out.println(new Gson().toJson(tag));
		*/
		// Prende tutti i task di un progetto...
		/*
		List<Tag> tags = asana.getTags();
		System.out.println(new Gson().toJson(tags));
		// Prende uno specifico TAG
		Tag tag = asana.getTag(idTag);
		System.out.println(new Gson().toJson(tag));
		// Prende tutti i task di un progetto...
		List<Task> tasks = asana.getTasks(idTag, AsanaSearchBy.TAG);
		
		System.out.println(new Gson().toJson(tasks));
		*/
	}
	
	private static void deleteAll(){
		Asana asana = new Asana(key);
		List<Task> tasks = asana.getTasks(idProject, AsanaSearchBy.PROJECT);
		for (Task task : tasks){
			System.out.println("Deleting : " + task.getId());
			asana.deleteTask(task);
		}
	}
	
	private static void createTask(){
		String url = "https://app.asana.com/";
		String key = "n3DEJPR.zh0CBzXwu5cm4fLl7fcmkBOG";
		Asana asana = new Asana(key);
		
		AsanaPost post = new AsanaPost();
		post.getData().put("workspace", idWorkspace);
		post.getData().put("projects", idProject);
		post.getData().put("name", "TEST API");
		post.getData().put("assignee", "1309739803347");
		post.getData().put("notes", "Questa è una prova!");

		
		/*
		Task task = new Task();
		task.setName("TEST API");
		task.setNotes("Questa è una prova!");
		task.setWorkspace(new Workspace());
		task.getWorkspace().setId("1309739803352");
		task.setAssignee(new User());
		task.getAssignee().setId("1309739803347");
		*/
		
		asana.createTask(post);
		
	}
	
	private static void testInvokePlainError(){
		String url = "https://app.asana.com/";
		String key = "n3DEJPR.zh0CBzXwu5cm4fLl7fcmkBOG";
		AsanaRestInterface restClient = RestClientFactory.connect(key);
		String response = restClient.getTask("7825284253219");
		System.out.println(response);
	}

	private static String printRequest(Request request){
		 JAXBElement<Request> JAXMyObjItems = new JAXBElement<Request>(_request_QNAME,Request.class,null,request);
		// create a Marshaller and marshal to System.out
	    ByteArrayOutputStream resultXml = new ByteArrayOutputStream();
	    JAXB.marshal( JAXMyObjItems, resultXml);
	    System.out.println("REQUEST:");
	    System.out.println(new String(resultXml.toByteArray()));
	    return new String(resultXml.toByteArray());
	    
	}
	
	private static void printResponse(Response response){
		 JAXBElement<Response> JAXMyObjItems = new JAXBElement<Response>(_response_QNAME,Response.class,null,response);
		// create a Marshaller and marshal to System.out
	    ByteArrayOutputStream resultXml = new ByteArrayOutputStream();
	    JAXB.marshal( JAXMyObjItems, resultXml);
	    System.out.println("RESPONSE:");
	    System.out.println(new String(resultXml.toByteArray()));
	    
	}
	
}
